package com.orangelife.mobile.lookRepair.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.widget.imagegridview.ImageGridView;

/**
 * 报修问题
 * 
 * @author Administrator
 * 
 */
public class RepairProblemActivity extends OrangeLifeBaseActivity {
	/** 图片查看 */
	private ImageGridView gvPhoto;
	/** 描述内容 */
	private TextView tvContent;
	/** 图片上的字 */
	private TextView tvRepair1;
	/** 图片下的字 */
	private TextView tvRepair2;
	/** 标题的字 */
	private TextView tvTitle;
	/**标题右边的字*/
	private TextView tvTitleRight;
	private List<Map<String, Object>> list;
	private Dialog dialog;
	private Map<String, Object> map;
	/** 哪一边传过来的（0保修问题1上门记录2跟进详情4抢单详情） */
	private int activ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_repair_problem);
		initView();
		findView();
	}

	private void initView() {
		gvPhoto = (ImageGridView) findViewById(R.id.gvPhoto);
		tvContent = (TextView) findViewById(R.id.tvContent);
		tvRepair1 = (TextView) findViewById(R.id.tvRepair1);
		tvRepair2 = (TextView) findViewById(R.id.tvRepair2);
		tvTitle = (TextView) findViewById(R.id.tv_title);
		tvTitleRight = (TextView)findViewById(R.id.tv_title_right);

	}

	private void findView() {
		tvTitleRight.setVisibility(View.INVISIBLE);
		Intent intent = getIntent();
		int active = intent.getIntExtra("lookDetail", 5);
		if (active == 1) {
			tvRepair1.setText("上门记录");
			tvRepair2.setText("师傅留言");
			tvTitle.setText("上门记录");
		} else if (active == 2) {
			tvRepair1.setText("工程情况");
			tvRepair2.setText("师傅留言");
			tvTitle.setText("跟进记录");
		} else if (active == 0) {
			tvTitle.setText("报修问题");
		} else if(active == 4){
			tvTitle.setText("报修问题");
		}
		dialog = DialogHelper.getInstance().createLoadingDialog(
				RepairProblemActivity.this, Constant.LOADING);
		dialog.show();
		getDetail();
		// setDetail();

	}

	/**
	 * 设置详情
	 */
	private void setDetail() {

		if (activ == 0 || activ == 4) {
			String pics = map.get("pics").toString();
			tvContent.setText(map.get("content").toString());
			gvPhoto.init(this, pics, "##");
		} else {
			String pics = map.get("followImg").toString();
			tvContent.setText(map.get("remark").toString());
			gvPhoto.init(this, pics, "##");
		}
	}

	/**
	 * 获得详情
	 * 
	 */
	private void getDetail() {
		String repairID = "";
		Intent intent = getIntent();
		repairID = intent.getStringExtra("followID");
		activ = intent.getIntExtra("lookDetail", 6);
		Map<String, Object> mapSubmit = new HashMap<String, Object>();
		if (activ == 0 || activ == 4) {
			mapSubmit.put("{invoiceID}", repairID);
		} else {
			mapSubmit.put("{followID}", repairID);
		}
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("map", mapSubmit);
		if (activ == 0 || activ == 4) {
			rootMap.put("url", Constant.URL_PROBLEM_);
		} else {
			rootMap.put("url", Constant.URL_RECORD);
		}
		rootMap.put("wat", 9);
		new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
	}

	/**
	 * 处理返回的信息数据
	 */
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 9:
				// list = new ArrayList<Map<String,Object>>();
				map = new HashMap<String, Object>();
				map = (Map<String, Object>) JSONHelper.jsonToMap(
						msg.obj.toString()).get("entity");
				DialogHelper.closeDialog(dialog);
				// list.add(map);
				setDetail();
				break;
			case 1011:
				isLogin(dialog);
				break;
			case 500:
				DialogHelper.closeDialog(dialog);
				ToastHelper.getInstance()._toast(
						msg.obj.toString());
				break;
			default:
				break;
			}
		}

	};
}
